import { _decorator, Component, EventTouch, instantiate, Node, NodeEventType, RichText, Size, SpriteAtlas, Tween, tween, UITransform, v3, view } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
//import { RichTextCheck } from '../../../A-FRAME/component/richtext.check';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
const { ccclass, property } = _decorator;

@ccclass('Ab_Livecomments_bA')
export class Ab_Livecomments_bA extends Component {
	@property(UITransform)
	private Ab_view_bA: UITransform = null;
	@property(UITransform)
	private Ab_container_sprite_bA: UITransform = null;
	@property(UITransform)
	private Ab_container_label_bA: UITransform = null;
	@property(Node)
	private Ab_node_prong_bA: Node = null;
	@property(SpriteAtlas)
	private Ab_atlas_channel_bA: SpriteAtlas = null;

	private Ab_type_bA: 'fold'|'unfold' = 'fold';
	private Ab_fold_size_bA: Size;
	private Ab_uitrans_bA: UITransform;
	private Ab_total_height_bA: number;

	private Ab_initedDone_bA: boolean = false;

	protected start(){
		this.Ab_init_bA();
		this.Ab_initRender_bA();
		this.Ab_listening_bA();
	}

	protected update(dt: number): void {
		if(this.frame_call_list.length>0){
			let [call, param, complete] = this.frame_call_list.pop();
			let result = call(...param);
			if(complete) complete(result, ...param);
		}
	}

	private Ab_init_bA(){
		this.Ab_uitrans_bA = this.node.getComponent(UITransform);
		this.Ab_fold_size_bA = this.Ab_uitrans_bA.contentSize.clone();
		this.Ab_type_bA = 'fold';
		this.node.on(NodeEventType.TOUCH_END, this.Ab_touchEnd_bA.bind(this));
	}

	private Ab_touchEnd_bA(event: EventTouch){
		if(this.Ab_type_bA == 'fold') this.Ab_unfold_bA();
		else this.Ab_fold_bA();
	}

	/** 折叠 */
	private Ab_fold_bA(){
		this.Ab_type_bA = 'fold';
		[this.Ab_uitrans_bA, this.Ab_view_bA].forEach(uitrans=>{
			Tween.stopAllByTarget(uitrans);
			tween(uitrans)
			.to(0.5, {width: this.Ab_fold_size_bA.width, height: this.Ab_fold_size_bA.height}, {easing: 'quartInOut'})
			.start();
		});

		Tween.stopAllByTarget(this.Ab_node_prong_bA);
		tween(this.Ab_node_prong_bA)
		.to(0.5, {scale: v3(1, -1, 1)}, {easing: 'quartInOut'})
		.set({scale: v3(1, -1, 1)}).start();

		abi.Ab_delay_bA(0.5, 'end')
		.then(()=>{
			let count = this.Ab_container_sprite_bA.node.children.length;
			for(let i = 0; i < count; i++){
				if(this.Ab_type_bA!='fold') break;

				this.Ab_container_sprite_bA.node.children[i].children[0].active = i<2;
				this.Ab_container_label_bA.node.children[i].children[0].active = i<2;
			}
		});
	}

	/** 展开 */
	private Ab_unfold_bA(){
		if(!this.Ab_initedDone_bA) return void 0;
		this.Ab_type_bA = 'unfold';

		[this.Ab_uitrans_bA, this.Ab_view_bA].forEach(ui=>{
			Tween.stopAllByTarget(ui);
			tween(ui)
			.to(0.5, {height: this.Ab_total_height_bA}, {easing: 'quartInOut'})
			.start();
		});

		Tween.stopAllByTarget(this.Ab_node_prong_bA);
		tween(this.Ab_node_prong_bA)
		.to(0.5, {scale: v3(1, 1, 1)}, {easing: 'quartInOut'})
		.set({scale: v3(1, 1, 1)}).start();

		abi.Ab_delay_bA(0, ()=>{
			if(this.Ab_type_bA!='unfold') return void 0;
			//this.Ab_container_label_bA.node.children.forEach(n=>n.children[0].getComponent(RichTextCheck).adaptThen().then(()=>n.children[0].active = true));
			this.Ab_container_label_bA.node.children.forEach(n=>n.children[0].active = true);
			this.Ab_container_sprite_bA.node.children.forEach(n=>n.children[0].active = true);
		});
	}

	private Ab_initRender_bA(){
		this.Ab_total_height_bA = view.getVisibleSize().height - 350;
		//this.Ab_total_height_bA = 800;
		let count = Math.ceil(this.Ab_total_height_bA / this.Ab_container_sprite_bA.height);

		this.Ab_frameWhile_bA(abi.Ab_array_bA.Ab_create_bA(count, i=>i+1), n=>[this.Ab_container_sprite_bA, this.Ab_container_label_bA].forEach(ui=>{
			let node = instantiate(ui.node.children[0]);
			node.setParent(ui.node);
			node.setPosition(node.position.x, -(n+1-node.getComponent(UITransform).anchorY) * ui.height, node.position.z);
			node.setSiblingIndex(n);
		})).then(()=>Ab_GameDC_bA.Ab_getLiveComments_bA(count+1).Ab_forWait_bA(async (data, i, ls)=>{
			await this.Ab_renderItem_bA(i, data);

			[this.Ab_container_label_bA, this.Ab_container_sprite_bA]
			.forEach(ui=>ui.node.children[i].children[0].active = false);

			Ab_GameDC_bA.Ab_Trigger_bA.Ab_LiveCommentsProgress_bA = (i+1) + ',' + ls.length;
		})).then(()=>{
			this.Ab_initedDone_bA = true;
			this.Ab_showNextLater_bA(1);
		});
	}

	private async Ab_renderItem_bA(index: number, data: ReturnType<typeof Ab_GameDC_bA.Ab_getLiveComments_bA>[number]){
		let sprite = this.Ab_container_sprite_bA.node.children[index].children[0];
		let richtext = this.Ab_container_label_bA.node.children[index].children[0].getComponent(RichText);
		[sprite, richtext.node].forEach(n=>n.active = false);
		richtext.string = Ab_GameAssets_bA.Ab_getLangText_bA(10,
			'<color=#FFF583>' + data.nickname + '</color>',
			'<color=#FF5A5A>' + data.count + '</color>',
			'<color=#5EF769>' + Ab_Config_bA.Ab_symbolString_bA(data.symbol) + '</color>',
		);
		abi.Ab_cc_bA.Ab_skinPeeler_bA(sprite, this.Ab_atlas_channel_bA.getSpriteFrame(data.channel), {width: true});

		//await richtext.getComponent(RichTextCheck).forceAdatptFontSize(false);

		richtext.node.active = this.Ab_type_bA == 'unfold';
	}

	private Ab_showNextLater_bA(duration: number){
		Ab_GameDC_bA.Ab_throwAwayLiveComments_bA();

		let time = duration * 0.5;

		let item_height = this.Ab_container_label_bA.node.children[0].getComponent(UITransform).height;
		let item_anchorY = this.Ab_container_sprite_bA.node.children[0].getComponent(UITransform).anchorY;

		let [target_pos_sprite, target_pos_label] = [this.Ab_container_sprite_bA, this.Ab_container_label_bA]
		.map(ui=>v3(ui.node.position.x, item_height, ui.node.position.z));

		[this.Ab_container_label_bA, this.Ab_container_sprite_bA].forEach(ui=>{
			Tween.stopAllByTarget(ui.node);
			[0, 1].forEach(i=>ui.node.children[i].children[0].active = true);
		});

		let np_sprite = new abi.Ab_NudityPromise_bA<'end'>();
		let np_label = new abi.Ab_NudityPromise_bA<'end'>();

		tween(this.Ab_container_sprite_bA.node)
		.delay(time).to(time, {position: target_pos_sprite})
		.call(()=>np_sprite.Ab_resolve_bA('end')).start();
		tween(this.Ab_container_label_bA.node)
		.delay(time).to(time, {position: target_pos_label})
		.call(()=>np_label.Ab_resolve_bA('end')).start();

		Promise.all([np_sprite.Ab_promise_bA, np_label.Ab_promise_bA])
		.then(()=>{
			[this.Ab_container_sprite_bA, this.Ab_container_label_bA].forEach(ui=>{
				ui.node.setPosition(ui.node.position.x, 0, ui.node.position.z);
				ui.node.children.forEach((item, i)=>item.setPosition(item.position.x, -(i-item_anchorY) * item_height, item.position.z));
			});

			return this.Ab_renderItem_bA(0, Ab_GameDC_bA.Ab_getLiveComments_bA().Ab_lastElement_bA);
		}).then(()=>{
			[this.Ab_container_sprite_bA, this.Ab_container_label_bA].forEach(ui=>{
				let node = ui.node.children[0];
				node.setPosition(node.position.x, -(ui.node.children.length-item_anchorY) * item_height, node.position.z);
				node.setSiblingIndex(ui.node.children.length-1);
				node.children[0].active = this.Ab_type_bA=='unfold';
			});
		}).then(()=>this.Ab_showNextLater_bA(duration));
	}

	private Ab_listening_bA(){
		let round_index = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;
		Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UITrans_bA', (_, enter)=>{
			if(enter && round_index!=Ab_GameDC_bA.Ab_User_bA.Ab_round_bA){
				round_index = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;
				this.Ab_refresh_bA();
			}
		}, false);
	}

	private Ab_refresh_bA(){
		Ab_GameDC_bA.Ab_throwAwayLiveComments_bA(Infinity);
		Ab_GameDC_bA.Ab_getLiveComments_bA(this.Ab_container_sprite_bA.node.children.length)
		.Ab_forWait_bA((data, i)=>this.Ab_renderItem_bA(i, data));
	}

	private frame_call_list: Array<[Function, any[], Function]> = [];
	/**
	 * 下一帧执行的回调
	 * @param call
	 * @param param
	 * @param complete
	 */
	private Ab_nextFrameCall_bA<C extends (...args: any)=>any, Param extends Parameters<C>, R extends ReturnType<C>>(call: C, param: Param, complete?: (r: R, ...args: Param)=>void){
		this.frame_call_list.unshift([call, param, complete]);
	}

	/**
	 * 帧循环
	 * @param list 要遍历的数组
	 * @param call 每次遍历的回调
	 */
	private Ab_frameWhile_bA<T extends any[]>(list: T, call: (el: T[number], index: number, arr: T)=>void){
		let np = new abi.Ab_NudityPromise_bA<'end'>();
		list.forEach((e, i, a)=>this.Ab_nextFrameCall_bA(call, [e, i, list], (_, e, i, a)=>i==a.length-1 ? np.Ab_resolve_bA('end') : void 0));
		return np.Ab_promise_bA;
	}
}

